package com.xxx.springboot.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.springboot.pojo.entity.User;
import com.xxx.springboot.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * swagger api文档
 * http://localhost:8081/swagger-ui.html
 */
@Api(tags="用户管理")
@RestController
public class SwaggerController {
	
	@Autowired
	private UserService userService;

	/**
	 * 用户列表接口
	 */
	@ApiOperation("用户列表")
	@GetMapping("/users")
	public IPage<User> list(
				@ApiParam("分页序号") int pageNo,
				@ApiParam("分页大小") int pageSize,
				@ApiParam("用户名") String userName){
    	LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    	if(!StringUtils.isEmpty(userName)) {
    		queryWrapper.likeRight(User::getUserName, userName);
    	}
    	IPage<User> page = new Page<>(pageNo, pageSize);
    	IPage<User> pageList = userService.page(page, queryWrapper);
        return pageList;
	}
	
	/**
	 * 用户详情
	 */
	@ApiOperation("用户详情")
	@GetMapping("/user/{id}")
	public User getById(@PathVariable Integer id) {
        try {
        	User user = userService.getById(id);
            return user;
        } catch (Exception e) {
            return null;
        }
    }
	
}
